<template>
  <view> </view>
</template>

<script>
import { wxMiniLoginByCode } from "@/api/auth.js";
import { mapState, mapActions } from "vuex";
// 本地部署机构ID
const originId = '';
export default {
  onLoad() {
    this.get_wx_login();
  },
  computed: {
    ...mapState("user", ["isLogin"]),
    ...mapActions("user", ["set_change_is_login"]),
  },
  methods: {
    // async loginFood() {
    //   const res = await getFoodToken();
    //   console.log(res.body, "food-token");
    //   uni.setStorageSync("foodToken", res.body);
    // },
    get_wx_login() {
      //静默登录
      uni.showLoading({
        title: "查询登录信息",
      });
      // if (this.isLogin) {
      //   this.loginFood();
      //   this.validateGlobalQuery();
      //   return;
      // }
      uni.login({
        provider: "weixin",
        success: (res) => {
          let data = {
            code: res.code,
          };
          wxMiniLoginByCode(data)
            .then((res) => {
              if (res.code == 0) {
                uni.setStorageSync("token", res.body.token); //存token
                // 是否 已注册
                uni.setStorageSync("is_user", !!res.body.phone);
                // if (!!res.body.memberVO) {
                let userInfo = {
                  ...res.body,
                  token: res.body?.token,
                  openId: res.body?.openId
                };
                // 先存用户信息
                uni.setStorageSync("userInfo", userInfo);
                this.userInfo = userInfo;
                // 设置登录状态
                this.$store.commit("user/setLoginState", true);

                setTimeout(() => {
                  uni.hideLoading();
                  // this.loginFood();
                  this.validateGlobalQuery();
                }, 500);
                // }
              }
            })
            .catch((e) => {
              console.error(e);
              uni.hideLoading();
            });
        },
        fail: () => {
          uni.hideLoading();
          setTimeout(() => {
            uni.showToast({
              title: "微信登录授权失败",
              icon: "none",
            });
          }, 1500);
        },
      });
    },
    getUrlParams3(url) {
      // \w+ 表示匹配至少一个(数字、字母及下划线), [\u4e00-\u9fa5]+ 表示匹配至少一个中文字符
      let pattern = /(\w+|[\u4e00-\u9fa5]+)=(\w+|[\u4e00-\u9fa5]+)/gi;
      let result = {};
      url.replace(pattern, ($, $1, $2) => {
        result[$1] = $2;
      });
      return result;
    },
    //检查参数，是否扫码进入
    validateGlobalQuery() {
      const pageQuery = getApp().globalData.pageQuery;
      uni.setStorageSync("orgId", pageQuery?.orgId || "L43Q4SG4");
      console.log("【登录验证进入小程序的全局参数】", pageQuery);
      setTimeout(() => {
        getApp().globalData.pageQuery = {}; //清空进入app的参数
      }, 0);
      //扫码点餐进入
      if (pageQuery?.scene) {
        const urlObj = this.getUrlParams3(decodeURIComponent(pageQuery.scene));
        if (urlObj.tableNo) {
          return uni.reLaunch({
            url: `/pages/index/index?tableId=${urlObj.tableNo}`,
          });
        }
      }
      //旅行社码扫码进入，缓存旅行社参数
      uni.setStorageSync("codeType", pageQuery?.codeType || "");
      uni.setStorageSync("travelId", pageQuery?.travelId || "");
      //---
      if(originId) {
        uni.setStorageSync("orgId", originId || "L43Q4SG4");
        uni.reLaunch({
          url: `/pages/index/index?orgId=${originId}`
        })
      } else {
        uni.reLaunch({
          url: pageQuery?.orgId
            ? `/pages/index/index?orgId=${pageQuery?.orgId}&active=2`
            : `/pages/orgList/index`,
        });
      }
    },
    //登录餐饮系统
  },
};
</script>

<style lang="scss" scoped></style>
